package com.ruoyi.common.utils.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtil {

    @Autowired
    private JavaMailSender mailSender;

    public void sendMail(String[] receiveUsers, String title, String content) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            mimeMessage.setFrom("ykyl@yikongenomics.com");
            int len = receiveUsers.length;
            InternetAddress address[] = new InternetAddress[len];
            for (int i=0;i<len;i++) {
                address[i] = new InternetAddress(receiveUsers[i]);
            }
            mimeMessage.addRecipients(Message.RecipientType.TO, address);
            mimeMessage.setSubject(title);
            mimeMessage.setText(content,"utf-8","html");
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMailOne(String receiveUser, String title, String content) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            mimeMessage.setFrom("ykyl@yikongenomics.com");
            mimeMessage.addRecipients(Message.RecipientType.TO, receiveUser);
            mimeMessage.setSubject(title);
            mimeMessage.setText(content,"utf-8","html");
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
